// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;


namespace DataModel
{
    public class FamilySource
    {
        // TODO - Doug - 8/6/2006 - come up with a better set of attributes for a source...
        private string title;
        private string id;


        public FamilySource(string id)
        {
            this.id = id;
        }


        public string Id
        {
            get { return this.id; }
        }


        public string Title
        {
            get
            {
                if (string.IsNullOrEmpty(this.title))
                {
                    return this.id;
                }
                else
                {
                    return this.title;
                }
            }
            set
            {
                this.title = value;
            }
        }


        public void SaveXml(XmlWriter writer)
        {
            writer.WriteStartElement("source");
            writer.WriteAttributeString("id", this.id);

            if (!string.IsNullOrEmpty(this.title))
            {
                writer.WriteElementString("title", this.title);
            }

            writer.WriteEndElement();
        }


        public void ParseXml(FamilyInfo familyInfo, XmlElement sourceElement, LoadResults loadResults)
        {
            foreach (XmlElement elem in sourceElement.SelectNodes("*"))
            {
                switch (elem.Name)
                {
                    case "title":
                        this.title = elem.InnerText;
                        break;

                    default:
                        loadResults.LogWarning("Unknown source element '{0}' found when parsing XML.", elem.Name);
                        break;
                }
            }

            if (string.IsNullOrEmpty(this.title))
            {
                loadResults.LogWarning("No title found for source '{0}'.", this.id);
            }
        }


        public override bool Equals(object obj)
        {
            // Simple cases...
            if (obj == null)
            {
                return false;
            }

            FamilySource other = obj as FamilySource;

            if (other == null)
            {
                return false;
            }

            // TODO - Doug - 9/2/2006 - take out the console.writeline calls; they are here to make the
            // unit test failures easier to track down...

            // Check members...
            if (!string.Equals(this.id, other.id))
            {
                Console.WriteLine("FamilySource.Equals: !string.Equals(this.id, other.id)...");
                return false;
            }

            return true;
        }


        public override int GetHashCode()
        {
            // TODO - Doug - 9/2/2006 - do we want to change this implementation at all?
            return base.GetHashCode();
        }

    }
}
